using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.IO;

namespace PublicDomain.Automation
{
    /// <summary>
    /// http://www.microsoft.com/downloads/details.aspx?FamilyId=3C9A983A-AC14-4125-8BA0-D36D67E0F4AD&displaylang=en
    /// http://support.microsoft.com/kb/302096
    /// http://msdn2.microsoft.com/en-us/library/af1c41xy(VS.80).aspx
    /// </summary>
    public static class Unprotect
    {
        public static int Main(string[] args)
        {
            int result = 0;

            if (args == null || args.Length < 3)
            {
                Console.Error.WriteLine(@"usage: unprotect [INPUT FILE] [PASSWORD] [OUTPUT FILE]");
                return 2;
            }

            try
            {
                string file = args[0], password = args[1], newfile = args[2];

                if (!File.Exists(file))
                {
                    Console.Error.WriteLine("Could not find input workbook " + file);
                }
                if (File.Exists(newfile))
                {
                    File.Delete(newfile);
                }

                ApplicationClass app = null;

                try
                {
                    app = new ApplicationClass();
                    app.Visible = false;
                    app.UserControl = false;

                    // http://msdn2.microsoft.com/en-us/library/aa195811.aspx
                    Workbook workbook = app.Workbooks.Open(
                        file,
                        2,
                        false,
                        Type.Missing,
                        password,
                        password,
                        true,
                        Type.Missing,
                        Type.Missing,
                        false,
                        false,
                        Type.Missing,
                        false,
                        true,
                        Type.Missing
                    );

                    // http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.unprotect(VS.80).aspx
                    workbook.Unprotect(password);

                    // http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(VS.80).aspx
                    workbook.SaveAs(
                        newfile,
                        Type.Missing,
                        null,
                        null,
                        false,
                        false,
                        XlSaveAsAccessMode.xlNoChange,
                        Type.Missing,
                        false,
                        Type.Missing,
                        Type.Missing,
                        false
                    );
                }
                finally
                {
                    if (app != null)
                    {
                        app.Quit();
                    }
                }
            }
            catch (Exception ex)
            {
                result = 1;
                Console.Error.WriteLine("Error: " + ex.Message + Environment.NewLine + ex.StackTrace);
            }

            return result;
        }
    }
}
